package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.MaterialDefinitionsModel;
import application.model.modify.MaterialDefinitionModel;
import application.translators.Translator;
import application.translators.impl.MaterialDefinitionsTranslator;
import dao.MaterialsDefinitionDao;
import mappings.MaterialDefinition;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("materialDefinitions")
public class MaterialDefinitionsService
{
    MaterialsDefinitionDao materialsDefinitionDao = new MaterialsDefinitionDao();
    Translator<List<MaterialDefinition>, MaterialDefinitionsModel> translator = new MaterialDefinitionsTranslator();

    @Path("getForMaterial/{materialId}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public MaterialDefinitionsModel getAllMaterialDefinitionsForMaterial(@PathParam("materialId") int materialId)
    {
        return translator.translate(materialsDefinitionDao.getAllMaterialDefinitionsForMaterial(materialId));
    }

    @Path("addMaterialDefinition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public MaterialDefinitionModel addNewMaterialDefinition(@FormParam("materialId") int materialId,
                                                            @FormParam("standardId") int standardId,
                                                            @FormParam("featureId") int featureId,
                                                            @FormParam("min") double min,
                                                            @FormParam("max") double max)
    {
        MaterialDefinitionModel materialDefinitionModel = new MaterialDefinitionModel();

        try
        {
            MaterialDefinition materialDefinition = materialsDefinitionDao.addMaterialDefinition(materialId, standardId, featureId, min, max);
            materialDefinitionModel.setStandard(materialDefinition.getStandards().getName());
            materialDefinitionModel.setFeatureName(materialDefinition.getFeatures().getName());
            materialDefinitionModel.setMin(materialDefinition.getMin());
            materialDefinitionModel.setMax(materialDefinition.getMax());
            materialDefinitionModel.setId(materialDefinition.getId());
            materialDefinitionModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            materialDefinitionModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            materialDefinitionModel.setStatus(2);
        }

        return materialDefinitionModel;
    }

    @Path("editMaterialDefinition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public MaterialDefinitionModel editProducent(@FormParam("id") Integer id,
                                        @FormParam("materialId") int materialId,
                                        @FormParam("standardId") int standardId,
                                        @FormParam("featureId") int featureId,
                                        @FormParam("min") double min,
                                        @FormParam("max") double max)
    {
        MaterialDefinitionModel materialDefinitionModel = new MaterialDefinitionModel();

        try
        {
            MaterialDefinition materialDefinition = materialsDefinitionDao.editMaterialDefinition(id, materialId, standardId, featureId, min, max);
            materialDefinitionModel.setStandard(materialDefinition.getStandards().getName());
            materialDefinitionModel.setFeatureName(materialDefinition.getFeatures().getName());
            materialDefinitionModel.setMin(materialDefinition.getMin());
            materialDefinitionModel.setMax(materialDefinition.getMax());
            materialDefinitionModel.setId(materialDefinition.getId());
            materialDefinitionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            materialDefinitionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            materialDefinitionModel.setStatus(2);
        }

        return materialDefinitionModel;
    }

    @Path("deleteMaterialDefinition")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public MaterialDefinitionModel deleteMaterialDefinition(@FormParam("id") Integer id)
    {
        MaterialDefinitionModel materialDefinitionModel = new MaterialDefinitionModel();

        try
        {
            materialsDefinitionDao.deleteMaterialDefinition(id);
            materialDefinitionModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            materialDefinitionModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            materialDefinitionModel.setStatus(2);
        }

        return materialDefinitionModel;
    }
}
